package com.greenline.push.common.cookie;

import javax.servlet.http.HttpServletRequest;

import com.greenline.framework.mursi.web.module.cookie.CookieModule;

/**
 * @Type CookieUtil
 * @Desc cookie工具类
 * @author jianyun.zheng
 * @date 2013-4-19
 * @Version V1.0
 */
public class CookieUtil {

    /**
     * 从cookie里获取对应key的内容
     * 
     * @param request
     * @param key
     * @return String
     */
    protected static String getCookieValue(HttpServletRequest request, String key) {
        if (request == null) {
            throw new IllegalArgumentException();
        }
        CookieModule jar = getCookieModule(request);
        if (jar == null) {
            return null;
        }
        return jar.get(key);
    }

    protected static CookieModule getCookieModule(HttpServletRequest request) {
        CookieModule jar = (CookieModule) request.getAttribute(CookieModule.COOKIE);
        return jar;
    }

    /**
     * 转String，为空返回null
     * 
     * @param val
     * @return
     */
    protected static String toString(Object val) {
        if (val == null) {
            return null;
        } else {
            return val.toString();
        }
    }

}
